package com.wl.api.controller.business;

import java.util.Date;
import java.util.Objects;
import java.util.function.Predicate;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.wl.api.common.utils.BeanUtils;
import com.wl.api.common.utils.ErrorCodeEnum;
import com.wl.api.common.utils.JsonObject;
import com.wl.api.common.utils.ObjectUtil;
import com.wl.api.common.utils.RWYException;
import com.wl.api.common.utils.StringUtil;
import com.wl.api.controller.base.CommonController;
import com.wl.api.dto.base.CommonUserDTO;
import com.wl.api.dto.business.CustomerFundDetailDTO;
import com.wl.api.model.business.CustomerFundDetail;
import com.wl.api.service.business.ICustomerFundDetailService;
import com.wl.api.service.business.ICustomerFundService;

@RestController
@RequestMapping("/external/customer/fund")
public class CustomerFundController extends CommonController {

	@Autowired
	ICustomerFundService fundService;
	@Autowired
	ICustomerFundDetailService fundDetailService;
	
	@RequestMapping(value="/manage", method = RequestMethod.POST)
	JsonObject create(@RequestBody CustomerFundDetailDTO param) throws Exception {
		JsonObject json = JsonObject.getInstance();
		String title = "", code =""; 
		boolean pass = true;
		if (validate().or(s -> s.getFundDetailId() != null).test(param)) {
			title = ErrorCodeEnum.param_error.getError_code();
			code = ErrorCodeEnum.param_error.getMessage();
			pass = false;
		}
		if (pass) {
			initData(param);
			CustomerFundDetail entity = BeanUtils.copyProperties(param, CustomerFundDetail.class);
			fundDetailService.insert(entity);
			json.setData(entity.getFundDetailId());
		}
		setJsonMessage(json, title, code, pass);
		return json;
	}
	/** 客户端只提供可修改待审核, 审核失败 的资金申请单  */
	@RequestMapping(value="/manage", method= RequestMethod.PUT)
	JsonObject modify(@RequestBody CustomerFundDetailDTO param) throws Exception {
		JsonObject json = JsonObject.getInstance();
		CommonUserDTO user = getCommonUser();
		String title = "", code =""; 
		boolean pass = true;
		if (validate().or(s -> s.getFundDetailId() == null).test(param)) {
			title = ErrorCodeEnum.param_error.getError_code();
			code = ErrorCodeEnum.param_error.getMessage();
			pass = false;
		}
		if(pass) {
			CustomerFundDetail detail = fundDetailService.selectOne(new EntityWrapper<CustomerFundDetail>().where("fund_detail_id", param.getFundDetailId()).and("cust_id", user.getCustId()));
			if (Objects.isNull(detail)) {
				throw RWYException.create(403);
			}
			if (Objects.equals(detail.getStatus(), 1)) {
				pass = false;
				code =ErrorCodeEnum.bad_request_published_data.getError_code();
				title = ErrorCodeEnum.bad_request_published_data.getMessage();
			}
		}
		if (pass) {
			initUpdateData(param);
			fundDetailService.updateById(BeanUtils.copyProperties(param, CustomerFundDetail.class));
			json.setData(param.getFundDetailId());
		}
		setJsonMessage(json, title, code, pass);
		return json;
	}

	
	@RequestMapping(value = "/manage", method=RequestMethod.GET)
	JsonObject getItemList(CustomerFundDetailDTO param) throws Exception {
		JsonObject json = JsonObject.getInstance();
		CommonUserDTO user = getCommonUser();
		param.setCustId(user.getCustId());
		json.setPage(fundDetailService.getItemList(param));
		setJsonMessage(json);
		return json;
	}
	
	private void initData(CustomerFundDetailDTO param) throws Exception {
		CommonUserDTO user = getCommonUser();
		param.setStatus(0);
		param.setCheckUserId(null);
		param.setCheckUserName(null);
		param.setCustId(user.getCustId());
	}
	
	private void initUpdateData(CustomerFundDetailDTO param) throws Exception {
		CommonUserDTO user = getCommonUser();
		param.setStatus(0);
		param.setCustId(user.getCustId());
		param.setCreateTime(new Date());
	}
	
	private Predicate<CustomerFundDetailDTO> validate() {
	 return s -> StringUtil.isEmpty(s.getAccountName()) || StringUtil.isEmpty(s.getAccountNumber())
			 || StringUtil.isEmpty(s.getBankName()) || StringUtil.isEmpty(s.getContactMobile()) 
			 || ObjectUtil.isEmptyOrLtZero(s.getAmount()) ||  	!ObjectUtil.isEmpty(s.getStatus());
	}
	
}
